package com.pdsu.alumni.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pdsu.alumni.common.result.Result;
import com.pdsu.alumni.pojo.Slideshow;
import com.pdsu.alumni.service.ISlideshowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Acerola
 * @since 2022-01-11
 */
@Api(tags = "轮播图控制层")
@Slf4j
@RestController
@RequestMapping("/slideshow")
public class SlideshowController {
    @Autowired
    private ISlideshowService slideshowService;

    @ApiOperation(value = "按照类别获取列表")
    @GetMapping("/list")
    @RequiresAuthentication
    @RequiresPermissions("message:slideshow")
    @ResponseBody
    @CrossOrigin
    public Result getAllSlideshow(String key) {
        QueryWrapper<Slideshow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s_type", key);
        List<Slideshow> list = slideshowService.list(queryWrapper);
        return new Result().success("获取成功").put(list);
    }

    @ApiOperation(value = "添加轮播图")
    @PostMapping("/add")
    @RequiresAuthentication
    @RequiresPermissions("message:slideshow")
    @ResponseBody
    @CrossOrigin
    public Result addSlideshow(@RequestBody Map map) {
        Object res = map.get("slideshow");
        Slideshow slideshow = JSON.parseObject(JSON.toJSONString(res), Slideshow.class);
        slideshow.setDeleted(1);
        boolean save = slideshowService.save(slideshow);
        if (!save) {
            return new Result().error("添加失败");
        }
        return new Result().success("添加成功");
    }

    @ApiOperation(value = "修改轮播图")
    @PostMapping("/update")
    @RequiresAuthentication
    @RequiresPermissions("message:slideshow")
    @ResponseBody
    @CrossOrigin
    public Result updateSlideshow(@RequestBody Map map) {
        Object res = map.get("slideshow");
        Slideshow slideshow = JSON.parseObject(JSON.toJSONString(res), Slideshow.class);
        System.out.println(slideshow);
        boolean update = slideshowService.updateById(slideshow);
        System.out.println(update);
        if (!update) {
            return new Result().error("修改失败");
        }
        return new Result().success("修改成功");
    }

    @ApiOperation(value = "删除轮播图")
    @PostMapping("/delete")
    @RequiresAuthentication
    @RequiresPermissions("message:slideshow")
    @ResponseBody
    @CrossOrigin
    public Result deleteSlideshow(int id) {

        Slideshow slideshow = slideshowService.getById(id);
        slideshow.setDeleted(0);
        boolean update = slideshowService.updateById(slideshow);
        if (!update) {
            return new Result().error("删除失败");
        }
        return new Result().success("删除成功");
    }

}
